package com.yc.demo.d0110;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//请求转发
@WebServlet("/forward")
public class ForwardServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if(req.getParameter("a") != null){
            //跳转到MathServlet
            //构建转发器
            final RequestDispatcher rd = req.getRequestDispatcher("/MathServlet");
            rd.forward(req,resp);

        }else if(req.getParameter("edu") != null || req.getParameter("likes") != null){

            final RequestDispatcher rd = req.getRequestDispatcher("/YouItemServlet");
            rd.forward(req,resp);

            //请求转发 跳转到  YoultemServlet
        }else{
            //相应重定向 到 result.html
            resp.sendRedirect("result.html");
        }
    }
}

/**
 * 请求转发和响应重定向的区别
 * 1.请求转发是内部跳转，响应重定向是外部跳转
 * 内部=>服务器，外部=>服务器外部
 * 浏览器
 * 2．请求转发:地址栏不变，响应重定向:改变
 * 3.请求转发:只能跳转服务器内部资源，响应重定向:可以跳转到外部资源
 */
